﻿using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace laohan.Common.Common
{
    public partial class API
    {
        /// <summary>  
        /// 修改程序在注册表中的键值  
        /// </summary>  
        /// <param name="isAuto">true:开机启动,false:不开机自启</param> 
        public static void AutoStart(bool isAuto = true)
        {
            try
            {
                var appPath = Process.GetCurrentProcess().MainModule.FileName;

                var appName = Path.GetFileName(appPath);

                if (isAuto == true)
                {
                    RegistryKey R_local = Registry.CurrentUser;
                    RegistryKey R_run = R_local.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run");

                    var value = R_run.GetValue(appName);

                    if (value != null)
                    {
                        return;
                    }

                    R_run.SetValue(appName, Application.ExecutablePath);
                    R_run.Close();
                    R_local.Close();
                }
                else
                {
                    RegistryKey R_local = Registry.CurrentUser;
                    RegistryKey R_run = R_local.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run");
                    R_run.DeleteValue(appName, false);
                    R_run.Close();
                    R_local.Close();
                }
            }
            catch (Exception)
            {
                throw new Exception("自动启动您需要管理员权限修改");
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public static bool IsAutoStart()
        {
            try
            {
                var appPath = Process.GetCurrentProcess().MainModule.FileName;

                var appName = Path.GetFileName(appPath);

                RegistryKey R_local = Registry.CurrentUser;
                RegistryKey R_run = R_local.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run");

                var value = R_run.GetValue(appName);

                R_run.Close();
                R_local.Close();

                if (string.IsNullOrEmpty(value?.ToString()))
                {
                    return false;
                }

                return true;
            }
            catch (Exception ex)
            {
                throw new Exception("判断自动启动error：" + ex.Message);
            }
        }

        /// <summary>
        /// 生成某个exe程序的快捷方式
        /// </summary>
        /// <param name="lnkFilePath">快捷方式的路径，例如：C:\Users\Public\Desktop\abc.lnk</param>
        /// <param name="targetPath">exe程序的全路径</param>
        /// <param name="args">快捷方式的快捷键</param>
        /// <returns></returns>
        public static bool CreateShortcut(string lnkFilePath, string targetPath, string args = "")
        {
            try
            {
                var shellType = Type.GetTypeFromProgID("WScript.Shell");
                dynamic shell = Activator.CreateInstance(shellType);
                var shortcut = shell.CreateShortcut(lnkFilePath);
                shortcut.TargetPath = targetPath;
                shortcut.Arguments = args;
                shortcut.WorkingDirectory = AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
                shortcut.Save();

                return true;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
    }
}